Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Update Happy_Number.js #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2024

Conversation

Alucard2169
Copy link
Contributor

Added base case check for Happy number problem.


About the fix.

7 Is also considered a Happy number, previously the code was checking if the array length is 1 and n was 1 then return true else false.

Copy link
Owner

@ignacio-chiazzo ignacio-chiazzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi thanks for contributing.

I think it's enough to check just for 1 since 7 will eventually hit n === 1. I like the example, we can merge that 👍.

@@ -40,7 +49,7 @@ function checkHappyNumber(n){
let strNumber = n.toString();
let splitNumber = strNumber.split("");
if(splitNumber.length <= 1){
return (n <= 1)? true:false;
return (n === 1 || n === 7)? true:false;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we'd need to check for 7 since it's already covered by n===1.

Suggested change
return (n === 1 || n === 7)? true:false;
return n === 1;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
Thanks for getting back to me, the thing is the first if condition checks whether the array's length is 1 or not,
in this case if the array has only 7 then it would be true, and since it is to return true if n===1 it would return false.

I have added some screenshots for tests please feel free to check them.

  • Not adding check for n===7
    Screenshot 2024-02-12 at 04-33-01 Happy Number - LeetCode

  • Adding checks
    Screenshot 2024-02-12 at 04-33-43 Happy Number - LeetCode

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohhhh true! Nice catch. We need to list all happy numbers that are less than 10.

@@ -40,7 +49,7 @@ function checkHappyNumber(n){
let strNumber = n.toString();
let splitNumber = strNumber.split("");
if(splitNumber.length <= 1){
return (n <= 1)? true:false;
return (n === 1 || n === 7)? true:false;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohhhh true! Nice catch. We need to list all happy numbers that are less than 10.

@ignacio-chiazzo ignacio-chiazzo merged commit 2b375b0 into ignacio-chiazzo:master Feb 12, 2024
@ignacio-chiazzo
Copy link
Owner

Thank you @Alucard2169

@Alucard2169
Copy link
Contributor Author

No problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants